package com.ruoyi.financial.controller;

import java.util.List;

import com.ruoyi.financial.domain.FThingType;
import com.ruoyi.financial.service.IFThingTypeService;
import com.ruoyi.system.domain.SysPost;
import com.ruoyi.system.service.ISysPostService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.financial.domain.FEnterXuni;
import com.ruoyi.financial.service.IFEnterXuniService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 非实物登记Controller
 * 
 * @author xunzhe
 * @date 2024-10-29
 */
@Controller
@RequestMapping("/financial/enterXuni")
public class FEnterXuniController extends BaseController
{
    private String prefix = "financial/enterXuni";

    @Autowired
    private IFEnterXuniService fEnterXuniService;

    @Autowired
    private ISysPostService postService;

    @Autowired
    private IFThingTypeService fThingTypeService;

    @RequiresPermissions("financial:enterXuni:view")
    @GetMapping()
    public String enterXuni()
    {
        return prefix + "/enterXuni";
    }

    /**
     * 查询非实物登记列表
     */
    @RequiresPermissions("financial:enterXuni:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(FEnterXuni fEnterXuni)
    {
        startPage();
        List<FEnterXuni> list = fEnterXuniService.selectFEnterXuniList(fEnterXuni);
        return getDataTable(list);
    }

    /**
     * 导出非实物登记列表
     */
    @RequiresPermissions("financial:enterXuni:export")
    @Log(title = "非实物登记", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(FEnterXuni fEnterXuni)
    {
        List<FEnterXuni> list = fEnterXuniService.selectFEnterXuniList(fEnterXuni);
        ExcelUtil<FEnterXuni> util = new ExcelUtil<FEnterXuni>(FEnterXuni.class);

        String title = "";
        if(fEnterXuni.getDeptId()!=null && list.size()>0){
            title = list.get(0).getDeptName();
        }

        if(fEnterXuni.getThingTypeId()!=null && list.size()>0){
            FThingType fThingType = fThingTypeService.selectFThingTypeByTypeId(fEnterXuni.getThingTypeId());
            title = title + fThingType.getTypeName();
        }

        return util.exportExcel(list, "非实物登记记录",title);
    }

    /**
     * 新增非实物登记
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存非实物登记
     */
    @RequiresPermissions("financial:enterXuni:add")
    @Log(title = "非实物登记", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(FEnterXuni fEnterXuni)
    {
        fEnterXuni.setDeptId(getSysUser().getDeptId());

        fEnterXuni.setCreateBy(getLoginName());
        List<SysPost> sysPosts = postService.selectPostsByUserId(getSysUser().getUserId());
        for (SysPost post : sysPosts) {
            if (post.isFlag()) {
                fEnterXuni.setPostId(post.getPostId());
            }
        }

        return toAjax(fEnterXuniService.insertFEnterXuni(fEnterXuni));
    }

    /**
     * 修改非实物登记
     */
    @RequiresPermissions("financial:enterXuni:edit")
    @GetMapping("/edit/{xuniId}")
    public String edit(@PathVariable("xuniId") Long xuniId, ModelMap mmap)
    {
        FEnterXuni fEnterXuni = fEnterXuniService.selectFEnterXuniByXuniId(xuniId);
        mmap.put("fEnterXuni", fEnterXuni);
        return prefix + "/edit";
    }

    /**
     * 修改保存非实物登记
     */
    @RequiresPermissions("financial:enterXuni:edit")
    @Log(title = "非实物登记", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(FEnterXuni fEnterXuni)
    {
        return toAjax(fEnterXuniService.updateFEnterXuni(fEnterXuni));
    }

    /**
     * 删除非实物登记
     */
    @RequiresPermissions("financial:enterXuni:remove")
    @Log(title = "非实物登记", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(fEnterXuniService.deleteFEnterXuniByXuniIds(ids));
    }
}
